package com.lanlian.cayman.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.cayman.R;
import com.lanlian.uikit.signture.SignaturePad;
import com.lanlian.utillib.DateUtils;
import com.lanlian.utillib.ImageUtils;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/27 14:39
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
public class SignatureActivity extends BaseActivity implements View.OnClickListener {

    private SignaturePad mSignatureView;
    private TextView mTvSave;
    private TextView mTvReset;
    private TextView mTvCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signature, true);
        initView();
        setListeners();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("手写签名");
        mSignatureView = findViewById(R.id.signature_view);
        mTvSave = findViewById(R.id.tv_save);
        mTvReset = findViewById(R.id.tv_reset);
        mTvCancel = findViewById(R.id.tv_cancel);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvSave.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
        mTvReset.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_save:
                if (mSignatureView.isEmpty()) {
                    showToast("请手写签名！");
                    return;
                }
                Bitmap bitmap = mSignatureView.getSignatureBitmap();
                if (bitmap != null) {
                    String fileName = DateUtils.longToString(System.currentTimeMillis(), "yyyyMMddHHmmssSSS") +
                            (int) ((Math.random() * 9 + 1) * 100);
                    String urlPath = ImageUtils.saveBitmap(this, bitmap, fileName + ".jpg");
                    Intent intent = new Intent();
                    intent.putExtra("urlPath", urlPath);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_reset:
                mSignatureView.clear();
                break;
            default:
                break;
        }
    }
}
